package com.cskaoyan.shiro;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.MallUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author harvey
 * @description: 有关微信端的认证和授权域
 * @Date 2021/7/11 15:29
 */
@Component
public class WxRealm extends AuthorizingRealm {

    @Autowired
    MallUserMapper mallUserMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken token = (MallToken) authenticationToken;
        String username = token.getUsername();

        MallUserExample mallUserExample = new MallUserExample();
        MallUserExample.Criteria criteria = mallUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<MallUser> mallUsers = mallUserMapper.selectByExample(mallUserExample);

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(mallUsers.get(0), mallUsers.get(0).getPassword(), this.getName());
        return authenticationInfo;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /*private AuthenticationInfo dealInfoByType(String type) {
        //根据info不同处理不同的认证信息
        return null;
    }*/
}
